﻿#region Using directives
using System;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProcolDemo.I18N;
using ProcolDemo.Majors;
using ProcolDemo.User;

#endregion

namespace ProcolDemo {
    public partial class Registration : Page {
        #region Gender enum
        public enum Gender {
            Male = 0,
            Female = 1
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e) {
            var majorsService = new MajorService();
            var majors = majorsService.GetAllMajors(Configuration.GetLanguage());
            var schools = majorsService.GetSchools();
            var I18N = new I18NService();

            lstMajor.Items.Clear();
            lstSchool.Items.Clear();
            lstGender.Items.Clear();

            foreach (var m in majors)
                lstMajor.Items.Add(new ListItem(m[1], m[0]));

            foreach (var s in schools)
                lstSchool.Items.Add(new ListItem(s.Str, s.Int.ToString()));

            lstGender.Items.Add(new ListItem(I18N.GetText(Configuration.GetLanguage(), "SEX_MALE"), "0"));
            lstGender.Items.Add(new ListItem(I18N.GetText(Configuration.GetLanguage(), "SEX_FEMALE"), "1"));
        }

        protected void wizRegister_FinishButtonClick(object sender, WizardNavigationEventArgs e) {
            var userService = new UserService();
            var textboxes = new[] { txtDOB, txtEMail, txtFirstName, txtLastName1, txtLastName2, txtUsername };

            foreach (var txt in textboxes) {
                if (String.IsNullOrEmpty(txt.Text))
                    return;
            }

            // Confirm passwords

            if (!String.Equals(ViewState["Pwd"].ToString(), ViewState["Pwd2"].ToString()))
                return;

            if (!isEmail(txtEMail.Text))
                return;

            // first name, lastname1/2, email, dob, gender
            // school, major
            // language

            var gender = (byte)(lstGender.SelectedValue.Equals("0") ? 0 : 1);

            userService.AddNewUser(txtUsername.Text, ViewState["Pwd"].ToString(), txtFirstName.Text, gender,
                                   DateTime.Parse(txtDOB.Text), txtLastName1.Text, txtLastName2.Text,
                                   lstSchool.SelectedValue, txtEMail.Text, lstMajor.SelectedValue,
                                   lstLanguage.SelectedValue);
        }

        private static bool isEmail(string inputEmail) {
            const string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" + @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                                    @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            var re = new Regex(strRegex);

            return re.IsMatch(inputEmail);
        }

        protected void wizRegister_ActiveStepChanged(object sender, EventArgs e) {
            if (!String.IsNullOrEmpty(txtPassword.Text))
                ViewState["Pwd"] = txtPassword.Text;

            if (!String.IsNullOrEmpty(txtConfirmPwd.Text))
                ViewState["Pwd2"] = txtConfirmPwd.Text;
        }
    }
}